package com.netease.nim.demo.session.emoji;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.shwread.android.qysw10000243.R;


public class EmoticonView {

	private ViewPager emotPager;
	private LinearLayout layoutBottom;
	/** 总页数. */
	private int pageCount;
	/** 每页显示的数量，Adapter保持一致. */
	public static final int EMOJI_PER_PAGE = 27; // 最后一个是删除键

	private Context context;
	private IEmojiSelectedListener listener;
	private EmoticonViewPaperAdapter pagerAdapter = new EmoticonViewPaperAdapter();

	public EmoticonView(Context context, IEmojiSelectedListener mlistener, ViewPager mCurPage,
			LinearLayout mLayoutBottom) {
		this.context = context.getApplicationContext();
		this.listener = mlistener;
		this.layoutBottom = mLayoutBottom;
		this.emotPager = mCurPage;

		emotPager.setOnPageChangeListener(new OnPageChangeListener() {

			@Override
			public void onPageSelected(int position) {
				setCurEmotionPage(position);
			}

			@Override
			public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
			}

			@Override
			public void onPageScrollStateChanged(int state) {
			}
		});
		emotPager.setAdapter(pagerAdapter);
		emotPager.setOffscreenPageLimit(1);
	}

	public void showEmojis() {
		showEmojiGridView();
	}

	private void showEmojiGridView() {
		pageCount = (int) Math.ceil(EmoUtil.getDisplayCount() / (float) EMOJI_PER_PAGE);
		pagerAdapter.notifyDataSetChanged();
		resetEmotionPager();
	}

	private void resetEmotionPager() {
		setCurEmotionPage(0);
		emotPager.setCurrentItem(0, false);
	}

	private void setCurEmotionPage(int position) {
		setCurPage(position, pageCount);
	}

	private void setCurPage(int page, int pageCount) {
		int hasCount = layoutBottom.getChildCount();
		int forMax = Math.max(hasCount, pageCount);

		ImageView imgCur = null;
		for (int i = 0; i < forMax; i++) {
			if (pageCount <= hasCount) {
				if (i >= pageCount) {
					layoutBottom.getChildAt(i).setVisibility(View.GONE);
					continue;
				} else {
					imgCur = (ImageView) layoutBottom.getChildAt(i);
				}
			} else {
				if (i < hasCount) {
					imgCur = (ImageView) layoutBottom.getChildAt(i);
				} else {
					imgCur = new ImageView(context);
					imgCur.setBackgroundResource(R.drawable.emoji_image_view_selector);
					layoutBottom.addView(imgCur);
				}
			}

			imgCur.setId(i);
			imgCur.setSelected(i == page); // 判断当前页码来更新
			imgCur.setVisibility(View.VISIBLE);
		}
	}

	public OnItemClickListener emojiListener = new OnItemClickListener() {
		public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
			int position = emotPager.getCurrentItem();
			int index = arg2 + position * EMOJI_PER_PAGE;

			if (listener != null) {
				int count = EmoUtil.getDisplayCount();
				if (arg2 == EMOJI_PER_PAGE || index >= count) {
					listener.onEmojiSelected("/DEL");
				} else {
					String text = EmoUtil.getDisplayText((int) arg3);
					if (!TextUtils.isEmpty(text)) {
						listener.onEmojiSelected(text);
					}
				}
			}
		}
	};

	/***
	 * ***************************** PagerAdapter ****************************
	 */
	private class EmoticonViewPaperAdapter extends PagerAdapter {
		@Override
		public boolean isViewFromObject(View view, Object object) {
			return view == object;
		}

		@Override
		public int getCount() {
			return pageCount == 0 ? 1 : pageCount;
		}

		@Override
		public Object instantiateItem(ViewGroup container, int position) {
			GridView gridView = new GridView(context);
			gridView.setOnItemClickListener(emojiListener);
			gridView.setAdapter(new EmojiAdapter(context, position * EMOJI_PER_PAGE));
			gridView.setNumColumns(7);
			gridView.setHorizontalSpacing(5);
			gridView.setVerticalSpacing(5);
			gridView.setGravity(Gravity.CENTER);
			gridView.setSelector(R.drawable.bg_emoji_selector);
			container.addView(gridView);
			return gridView;
		}

		@Override
		public void destroyItem(ViewGroup container, int position, Object object) {
			View layout = (View) object;
			container.removeView(layout);
		}

		public int getItemPosition(Object object) {
			return POSITION_NONE;
		}
	}
}
